using System;
using System.Collections.Generic;
using System.Linq;
using WWB.SitePointPrivateMessages.Core.Infrastructure.Logging;
using WWB.SitePointPrivateMessages.Core.Model.DataImport;

namespace WWB.SitePointPrivateMessages.Core.Model.Messages
{
    public class Folder
    {
        public Folder(){}

        public Folder(string name, IEnumerable<PrivateMessage> messages)
        {
            Name = name;
            this.messages = new List<PrivateMessage>(messages);
        }

        private List<PrivateMessage> messages = new List<PrivateMessage>(); 
        public string Name { get; set; }
        public IList<PrivateMessage> Messages { get { return messages; } }

        public IEnumerable<ForumUser> GetUsersInFolder()
        {
            var usr = Messages.SelectMany(x => x.ToUsers).Union(Messages.Select(x => x.FromUser)).Distinct();
            return usr;
        }

        public ExportedFolder ConvertToExport(ILogger outputLog)
        {
            outputLog.LogMessage("Starting export of folder '{0}'.", Name);
            outputLog.LogMessage("There are {0} messages to convert.", Messages.Count);
            var convertedMessages =new List<ExportedMessage>(Messages.Count);
            foreach (var msg in Messages)
            {
                convertedMessages.Add(msg.ConvertToExport(outputLog));
            }
            ExportedFolder ef = new ExportedFolder
                                    {
                                        Name = Name,
                                        Messages = convertedMessages.ToArray()
                                    };
            outputLog.LogMessage("Done converting folder '{0}'", Name);
            return ef;
        }
    }
}